<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
			<el-form-item label="主键" prop="id" v-if="false">
				<el-input v-model="dataForm.id" placeholder="主键"></el-input>
			</el-form-item>

			<el-row>
				<el-col :span="12">
					<el-form-item label="姓名" prop="name">
						<el-input v-model="dataForm.name" placeholder="请输入"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="工号" prop="code">
						<el-input v-model="dataForm.code" placeholder="" disabled="true"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="12">
					<el-form-item label="部门组织" prop="organizationId">
						<el-tree-select
							v-model="dataForm.organizationId"
							:data="organList"
							value-key="id"
							check-strictly
							:render-after-expand="false"
							:props="{ value: 'id', label: 'abbreviation', children: 'children' }"
							style="width: 100%"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="上级负责人" prop="superiorId">
						<el-input v-model="dataForm.superiorId" placeholder="上级负责人"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row
				><el-col :span="12">
					<el-form-item label="职位" prop="positionId">
						<el-tree-select
							v-model="dataForm.positionId"
							:data="positionList"
							value-key="id"
							check-strictly
							:render-after-expand="false"
							:props="{ value: 'id', label: 'name', children: 'children' }"
							style="width: 100%"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="手机号码" prop="phone">
						<el-input
							v-model="dataForm.phone"
							placeholder="请输入"
							maxLength="11"
							onkeyup="value=value.replace(/[^\d]/g,'')"
							@input="fnCode(dataForm.phone)"
						>
						</el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row
				><el-col :span="12">
					<el-form-item label="工作地点" prop="workplaceId">
						<el-tree-select
							v-model="dataForm.workplaceId"
							:data="workplaceList"
							value-key="id"
							check-strictly
							:render-after-expand="false"
							:props="{ value: 'id', label: 'workplace', children: 'children' }"
							style="width: 100%"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="工作城市" prop="workplaceStr">
						<el-input v-model="dataForm.workplaceStr" placeholder="" disabled="true" />
					</el-form-item>
				</el-col>
			</el-row>
			<el-row
				><el-col :span="12">
					<el-form-item label="员工类型" prop="type">
						<el-tree-select
							v-model="dataForm.type"
							:data="typeList"
							value-key="id"
							check-strictly
							:render-after-expand="false"
							:props="{ value: 'id', label: 'name', children: 'children' }"
							style="width: 100%"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="员工状态" prop="state">
						<el-tree-select
							v-model="dataForm.state"
							:data="stateList"
							value-key="id"
							check-strictly
							:render-after-expand="false"
							:props="{ value: 'id', label: 'name', children: 'children' }"
							style="width: 100%"
						/>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row
				><el-col :span="12">
					<el-form-item label="入职日期" prop="hiredate">
						<ElDatePicker v-model="dataForm.hiredate" placeholder="请选择"></ElDatePicker>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="邮箱" prop="postcode">
						<el-input v-model="dataForm.postcode" placeholder="请输入"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row
				><el-col :span="12">
					<el-form-item label="员工状态" prop="isEnable">
						<el-switch
							v-model="dataForm.isEnable"
							:active-value="1"
							:inactive-value="0"
							active-text="启用"
							inline-prompt
							inactive-text="停用"
							placeholder=""
						/>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus/es'
import { useStaffApi, useStaffSubmitApi, findWorkplace, findOrgan } from '@/api/nb/staff/staff'
import { findAll } from '@/api/nb/organization/position'
import { findByPcode } from '@/api/nb/business/dictionaries_subset'
import { dateYYMMDD, fnDatefmt } from '@/hooks'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
	id: '',
	name: '',
	code: '',
	organizationId: '',
	superiorId: '',
	positionId: '',
	phone: '',
	workplaceId: '',
	workplaceStr: '',
	type: '',
	state: '',
	hiredate: '',
	postcode: '',
	isEnable: 1
})

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	if (id) {
		getStaff(id)
	}

	getWorkplace()
	getOrgan()
	getPosition()
	findDictByCode('YuanGongLeiXing', typeList)
	findDictByCode('YuanGongZhuangTai', stateList)
}

const workplaceList = ref([])
const organList = ref([])
const positionList = ref([])
const typeList = ref([])
const stateList = ref([])

// 获取 工作地点
const getWorkplace = () => {
	return findWorkplace().then(res => {
		workplaceList.value = res.data
	})
}

// 获取 组织部门
const getOrgan = () => {
	return findOrgan().then(res => {
		organList.value = res.data
	})
}

// 获取 组织部门
const getPosition = () => {
	return findAll().then(res => {
		positionList.value = res.data
	})
}

//处理pid
const findDictByCode = (code: string, dataList: any) => {
	return findByPcode(code).then(res => {
		dataList.value = res.data.list
	})
}

const getStaff = (id: number) => {
	useStaffApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		useStaffSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

const fnCode = (phone: string) => {
	let p=phone.replace(/[^\d]/g,'')
	if (p.length > 4) {
		dataForm.code = dateYYMMDD + p.substring(p.length - 4)
	}
}

defineExpose({
	init
})
</script>
